using Game.Main.Model;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class RotateItem : BaseItem
	{
		private int state;

		private bool turnLeft;

		private int[] angles;

		private bool isPlayAnim;

		private float curVal;

		private float originalVal;

		private float targetVal;

		private float timer;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			if (string.IsNullOrEmpty(base.itemData.scriptParams))
			{
				Trace.LogError(base.itemData.id + " 未配置旋转参数");
			}
			else
			{
				string[] array = base.itemData.scriptParams.Split(',');
				angles = new int[array.Length];
				for (int i = 0; i < array.Length; i++)
				{
					angles[i] = int.Parse(array[i]);
				}
			}
			turnLeft = data.intParam1 == 1;
			SetState(data.intParam2);
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (isPlayAnim)
			{
				timer += Time.deltaTime * 4f;
				curVal = Mathf.LerpAngle(originalVal, targetVal, timer);
				model.SetLocalRotationZ(curVal);
				if (timer >= 1f)
				{
					originalVal = 0f;
					targetAngle = 0f;
					isPlayAnim = false;
				}
			}
		}

		public override void PlayClickSound()
		{
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (isPlayAnim || angles.Length <= 1)
			{
				return;
			}
			isPlayAnim = true;
			timer = 0f;
			if (turnLeft)
			{
				originalVal = model.localEulerAngles.z;
				state--;
				if (state < 0)
				{
					turnLeft = false;
					state = 1;
				}
				targetVal = angles[state];
			}
			else
			{
				originalVal = model.localEulerAngles.z;
				state++;
				if (state >= angles.Length)
				{
					turnLeft = true;
					state = angles.Length - 2;
				}
				targetVal = angles[state];
			}
			base.PlayClickSound();
		}

		public void SetState(int val)
		{
			state = val;
			if (state >= angles.Length)
			{
				state = 0;
			}
			model.SetLocalRotationZ(angles[state]);
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["59"] = (turnLeft ? 1 : 0);
			obj["60"] = state;
		}
	}
}
